package com.sxbbc.api.controller;

import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.service.redis.RedisUtilsService;
import com.sxbbc.common.basics.utils.validcode.VerifyCode;
import com.sxbbc.common.core.dto.jz.LoginRegisterVo;
import com.sxbbc.common.core.dto.jz.WxBindVo;
import com.sxbbc.common.core.service.system.login.LoginTokenService;
import com.sxbbc.common.core.utils.MsgUtils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.api.common.MappingUtils;
import com.sxbbc.api.service.IRegisterService;
import com.sxbbc.common.basics.login.ThirdLoginDto;
import com.sxbbc.common.basics.sms.SendSms;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

@RestController

public class RegisterController {
	
	@Autowired
	private IRegisterService registerService;
	@Autowired
	private LoginTokenService loginTokenService;
	@Autowired
	private RedisUtilsService redisUtilsService;

	/**
	 * 生成图形验证码
	 * @param response
	 */
	@RequestMapping(MappingUtils.VERIFY_IMAGE)
	public void validCode(String login, HttpServletResponse response) {
		if(StringUtils.isEmpty(login)) {
			throw new ThrowJsonException("未获取到账户信息");
		}
		try {
			// 创建验证码对象
			VerifyCode verifyCode = VerifyCode.getInstance();
			// 创建图片缓存区
			BufferedImage image = verifyCode.getImage();
			redisUtilsService.setKey("validcode_"+login, verifyCode.getText(), 180);//验证码180s过期
			VerifyCode.output(image, response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
		throw new ThrowJsonException("生成失败");
	}

	/**
	 * 发送注册验证码
	 * @param loginRegisterVo type 1手机号 2邮箱 3账号
	 * @return
	 */
	@RequestMapping(value=MappingUtils.SEND_REGISTER_CODE)
	public RestResponse sendRegisterCode(LoginRegisterVo loginRegisterVo) {
		return registerService.sendRegisterCode(loginRegisterVo);
	}
	
	/**
	 * 注册
	 * @param loginRegisterVo type 1手机号 2邮箱 3账号
	 *                   password 密码
	 *                   confirmPassword 确认密码refereeId 推荐人Id
	 *                   refereeId 推荐人Id
	 * @return
	 */
	@RequestMapping(value=MappingUtils.HANDLE_REGISTER)
	public RestResponse handleRegister(LoginRegisterVo loginRegisterVo) {
		return registerService.handleRegister(loginRegisterVo, null);
	}
	
	/**
	 * 公众号，小程序用户绑定账户
	 * @param bindVo 绑定信息
	 * @return 处理结果
	 */
	@RequestMapping(value = MappingUtils.HANDLE_WX_USER_BIND)
	public RestResponse handleWxUserBind (WxBindVo bindVo) {
		if (bindVo == null) {
			return GetRest.getFail("绑定失败");
		}
		return registerService.handleWxUserBind(bindVo);
	}
	
	/**
	 * 发送绑定手机号验证码
	 * @param phone 手机号
	 * @return
	 */
	@RequestMapping(value = MappingUtils.SEND_BIND_PHONE)
	public RestResponse sendBindPhone (String phone) {
		return SendSms.getInstance().sendSmsAliyun(phone, MsgUtils.ALI_SMS_BIND_PHONE);
	}
	
	/**
	 * 第三方账户绑定——无账号
	 * @param thirdLoginDto 友盟获取用户信息
	 * @param registerVo
	 * @return
	 * @author xiaoky
	 */
	@RequestMapping(value = MappingUtils.THIRD_NO_ACCOUNT_BIND)
	public RestResponse thirdNoAccountBind (ThirdLoginDto thirdLoginDto, LoginRegisterVo registerVo) {
		RestResponse result = registerService.handleThirdNoAccountBind(thirdLoginDto, registerVo);
		return result;
	}
	
	/**
	 * 第三方账户绑定——有账号
	 * @param thirdLoginDto 友盟获取用户信息
	 * @param token 获取cid
	 * @param phone 绑定账户的手机号
	 * @param password 绑定账户的密码
	 * @return
	 * @author xiaoky
	 */
	@RequestMapping(value = MappingUtils.THIRD_ACCOUNT_BIND)
	public RestResponse thirdAccountBind (ThirdLoginDto thirdLoginDto, String token, String phone, String password) {
		Integer cid = null;
		if (!StringUtils.isBlank(token)) {
			cid = loginTokenService.validLoginCid(token);
		}
        if (!StringUtils.isBlank(token) && cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
		return registerService.handleThirdAccountBind(thirdLoginDto, cid, phone, password);
	}

}
